/**
 * 
 */
package dms.presentation.action.supervisor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.dom4j.Document;
import org.dom4j.Element;

import dms.model.Building;
import dms.model.form.PurchaseItem;
import dms.model.form.Repair;
import dms.model.role.DormSupervisor;
import dms.model.role.wrapper.DormSupervisorWrapper;
import dms.model.util.PageIterator;
import dms.service.PurchaseItemQueryService;
import dms.service.RepairQueryService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.PageUtil;
import dms.util.XMLUtil;

/**
 * @author 李健
 *
 */
public class PropertyWorkAction extends MappingDispatchAction {
	private static Log log = LogFactory.getLog(PropertyWorkAction.class);
	
	public ActionForward repairList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		DormSupervisor dormSupervisor = wrapper.getDormSupervisor();
		RepairQueryService repairQueryService = (RepairQueryService) ContextUtil.getContext().getBean("repairQueryService");
		Building building = wrapper.getDormSupervisor().getBuilding();
		PageIterator<Repair> repairs = null;
		Integer start = null;
		Integer count = null;
		int[] params = null;
		try {
			params = PageUtil.parsePageParameter(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		start = params[0];
		count = params[1];
		
		repairs = repairQueryService.findByBuilding(start, count, building);
		request.setAttribute("repairs", repairs);
		request.setAttribute("dormSupervisor", dormSupervisor);
		return mapping.findForward(Constant.SUCCESS);
	}
	
	
	public ActionForward purchaseList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Integer start = null;
		Integer count = null;
		int[] params = null;
		PageIterator<PurchaseItem> purchases = null;
		Building building = null;
		
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		PurchaseItemQueryService purchaseItemQueryService = (PurchaseItemQueryService) ContextUtil.getContext().getBean("purchaseItemQueryService");
		DormSupervisor dormSupervisor = wrapper.getDormSupervisor();
		
		try {
			params = PageUtil.parsePageParameter(request);
			start = params[0];
			count = params[1];
			
			building = wrapper.getDormSupervisor().getBuilding();
			purchases = purchaseItemQueryService.findByBuilding(start, count, building, null, null, null, null);
			
			request.setAttribute("purchases", purchases);
			request.setAttribute("dormSupervisor", dormSupervisor);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new Exception("找不到该楼栋的购置单");
		}
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward processRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long repairId = null;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		StringBuilder responseXML = new StringBuilder();
		try {
			repairId = Long.valueOf(request.getParameter("id"));
			dormSupervisorWrapper.processRepair(repairId);
			responseXML.append("<response>");
			responseXML.append("<repair>");
			responseXML.append("<process>");
			responseXML.append("true");
			responseXML.append("</process>");
			responseXML.append("<message>");
			responseXML.append("维修单" + repairId + "已成功受理");
			responseXML.append("</message>");
			responseXML.append("</repair>");
			responseXML.append("</response>");
		} catch (Exception e) {
			log.error(e.getMessage());
			responseXML.append("<response>");
			responseXML.append("<repair>");
			responseXML.append("<process>");
			responseXML.append("false");

			responseXML.append("</process>");
			responseXML.append("<error-message>");
			responseXML.append("错误：维修单" + repairId + "或其它系统原因");
			responseXML.append("</error-message>");
			responseXML.append("</repair>");
			responseXML.append("</response>");
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		writer.print(responseXML.toString());
		return null;
	}
	
	public ActionForward cancelProcessRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Long repairId = null;
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Document document = XMLUtil.createEncodingDocument("UTF-8");
		
		try {
			repairId = Long.valueOf(request.getParameter("id"));
			wrapper.cancelProcessRepair(repairId);
			
			Element repairElement = document.addElement("response")
									 .addElement("repair");
			repairElement.addElement("process").addText("true");
			repairElement.addElement("message").addText("维修单ID:" + repairId + "取消受理成功");
			
		} catch (Exception e) {
			Element repairElement = document.addElement("response")
											.addElement("repair");
			repairElement.addElement("process").addText("false");
			repairElement.addElement("error-message").addText("维修单ID:" + repairId + "取消处理失败;"
															  + e.getMessage());
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		writer.print(document.asXML());
		return null;
	}
	

	public ActionForward applyRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long repairId = null;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		StringBuilder responseXML = new StringBuilder();
		
		try {
			repairId = Long.valueOf(request.getParameter("id"));
			dormSupervisorWrapper.applyRepair(repairId);
			
			responseXML.append("<response>");
			responseXML.append("<repair>");
			responseXML.append("<apply>");
			responseXML.append("true");
			responseXML.append("</apply>");
			responseXML.append("<message>");
			responseXML.append("维修单" + repairId + "已处理");
			responseXML.append("</message>");
			responseXML.append("</repair>");
			responseXML.append("</response>");
		} catch (Exception e) {
			log.error(e.getMessage());
			responseXML.append("<response>");
			responseXML.append("<repair>");
			
			responseXML.append("<apply>");
			responseXML.append("false");
			responseXML.append("</apply>");
			
			responseXML.append("<error-message>");
			responseXML.append("错误：维修单" + repairId + "或其它系统原因");
			responseXML.append("</error-message>");
			
			responseXML.append("</repair>");
			responseXML.append("</response>");
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		writer.print(responseXML.toString());
		return null;
	}
	
	public ActionForward cancelApplyRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Long repairId = null;
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Document document = XMLUtil.createEncodingDocument("UTF-8");
		Element repairElement = document.addElement("response").addElement("repair");
		
		try {
			repairId = Long.valueOf(request.getParameter("id"));
			wrapper.cancelApplyRepair(repairId);
			
			repairElement.addElement("apply").addText("true");
			repairElement.addElement("message").addText("维修单ID:" + repairId + "取消处理成功");
		} catch (Exception e) {
			repairElement.addElement("apply").addText("false");
			repairElement.addElement("error-message").addText("维修单ID：" + repairId + "取消处理失败;"
															  + e.getMessage()); 
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(document.asXML());
		return null;
	}
	
	public ActionForward deleteRepair(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long repairId = null;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		StringBuilder responseXML = new StringBuilder();
		
		try {
			repairId = Long.valueOf(request.getParameter("id"));
			dormSupervisorWrapper.deleteRepairById(repairId);
			
			responseXML.append("<response>");
			responseXML.append("<repair>");
			
			responseXML.append("<delete>");
			responseXML.append("true");
			responseXML.append("</delete>");
			
			responseXML.append("<message>");
			responseXML.append("维修单" + repairId + "删除成功");
			responseXML.append("</message>");
			
			responseXML.append("</repair>");
			responseXML.append("</response>");
		} catch (Exception e) {
			log.error(e.getMessage());
			
			responseXML.append("<response>");
			responseXML.append("<repair>");
			
			responseXML.append("<delete>");
			responseXML.append("false");
			responseXML.append("</delete>");
			
			responseXML.append("<error-message>");
			responseXML.append("错误：维修单" + repairId + "或其它系统原因");
			responseXML.append("</error-message>");
			
			responseXML.append("</repair>");
			responseXML.append("</response>");
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		writer.print(responseXML.toString());
		return null;
	}
	
	public ActionForward processPurchase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Long purchaseItemId = null;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		StringBuilder responseXML = new StringBuilder();
		responseXML.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
		try {
			purchaseItemId = Long.valueOf(request.getParameter("id"));
			dormSupervisorWrapper.processPurchaseItem(purchaseItemId);
			
			responseXML.append("<response>");
			responseXML.append("<purchaseitem>");
			
			responseXML.append("<process>");
			responseXML.append("true");
			responseXML.append("</process>");
			
			responseXML.append("<message>");
			responseXML.append("购置单" + purchaseItemId + "受理成功");
			responseXML.append("</message>");
			
			responseXML.append("</purchaseitem>");
			responseXML.append("</response>");
		} catch (Exception e) {
			log.error(e.getMessage());
			
			responseXML.append("<response>");
			responseXML.append("<purchaseitem>");
			
			responseXML.append("<process>");
			responseXML.append("false");
			responseXML.append("</process>");
			
			responseXML.append("<message>");
			responseXML.append("错误： 购置单" + purchaseItemId + "或其它系统原因");
			responseXML.append("</message>");
			
			responseXML.append("</purchaseitem>");
			responseXML.append("</response>");
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println(responseXML.toString());
		
		return null;
	}
	
	public ActionForward cancelProcessPurchase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long purchaseItemId = null;
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Document document = XMLUtil.createEncodingDocument("UTF-8");
		Element repairElement = document.addElement("response").addElement("purchaseitem");
		
		try {
			purchaseItemId = Long.valueOf(request.getParameter("id"));
			wrapper.cancelProcessPurchase(purchaseItemId);
			
			repairElement.addElement("process").addText("true");
			repairElement.addElement("message").addText("购置单ID:" + purchaseItemId + "取消受理成功");
		} catch (Exception e) {
			repairElement.addElement("process").addText("false");
			repairElement.addElement("error-message").addText("购置单ID:" + purchaseItemId + "取消受理失败;"
														 + e.getMessage());	
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(document.asXML());
		return null;
	}
	
	public ActionForward cancelApplyPurchase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Long purchaseItemId = null;
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Document document = XMLUtil.createEncodingDocument("UTF-8");
		Element repairElement = document.addElement("response").addElement("purchaseitem");
		
		try {
			purchaseItemId = Long.valueOf(request.getParameter("id"));
			wrapper.cancelApplyPurchase(purchaseItemId);
			
			repairElement.addElement("apply").addText("true");
			repairElement.addElement("message").addText("购置单ID:" + purchaseItemId + "取消处理成功"); 
		} catch (Exception e) {
			repairElement.addElement("apply").addText("false");
			repairElement.addElement("error-message").addText("购置单ID：" + purchaseItemId + "取消处理失败；"
					  									 + e.getMessage());
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(document.asXML());
		return null;
	}
	
	public ActionForward applyPurchase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Long purchaseItemId = null;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		StringBuilder responseXML = new StringBuilder();
		responseXML.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");

		try {
			purchaseItemId = Long.valueOf(request.getParameter("id"));
			dormSupervisorWrapper.applyPurchaseItem(purchaseItemId);
			
			responseXML.append("<response>");
			responseXML.append("<purchaseitem>");
			
			responseXML.append("<apply>");
			responseXML.append("true");
			responseXML.append("</apply>");
			
			responseXML.append("<message>");
			responseXML.append("购置单" + purchaseItemId + "处理完毕");
			responseXML.append("</message>");
			
			responseXML.append("</purchaseitem>");
			responseXML.append("</response>");
		} catch (Exception e) {
			log.error(e.getMessage());
			
			responseXML.append("<response>");
			responseXML.append("<purchaseitem>");
			
			responseXML.append("<apply>");
			responseXML.append("false");
			responseXML.append("</apply>");
			
			responseXML.append("<message>");
			responseXML.append("错误： 购置单" + purchaseItemId + "处理出错");
			responseXML.append("</message>");
			
			responseXML.append("</purchaseitem>");
			responseXML.append("</response>");
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println(responseXML.toString());
		
		return null;
	}
	
	public ActionForward deletePurchase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long purchaseItemId = null;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		StringBuilder responseXML = new StringBuilder();
		responseXML.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");

		try {
			purchaseItemId = Long.valueOf(request.getParameter("id"));
			dormSupervisorWrapper.deletePurchaseItemById(purchaseItemId);
			
			responseXML.append("<response>");
			responseXML.append("<purchaseitem>");
			
			responseXML.append("<delete>");
			responseXML.append("true");
			responseXML.append("</delete>");
			
			responseXML.append("<message>");
			responseXML.append("购置单" + purchaseItemId + "删除成功");
			responseXML.append("</message>");
			
			responseXML.append("</purchaseitem>");
			responseXML.append("</response>");
		} catch (Exception e) {
			log.error(e.getMessage());
			
			responseXML.append("<response>");
			responseXML.append("<purchaseitem>");
			
			responseXML.append("<delete>");
			responseXML.append("false");
			responseXML.append("</delete>");
			
			responseXML.append("<message>");
			responseXML.append("错误： 购置单" + purchaseItemId + "不能删除或其它原因");
			responseXML.append("</message>");
			
			responseXML.append("</purchaseitem>");
			responseXML.append("</response>");
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println(responseXML.toString());
		
		return null;
		
	}
	
	/* 还有人员，物品出入列表登记 */
}
